package com.gua.jbs.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gua.jbs.pojo.Jbspit;
import com.gua.jbs.service.IActionOnProductService;
import com.gua.jbs.service.IAllProductService;
import com.gua.jbs.service.IProductService;
import com.gua.jbs.serviceImpl.ActionOnProductService;
import com.gua.jbs.serviceImpl.AllProductService;
import com.gua.jbs.serviceImpl.ProductService;
import com.gua.jbs.utility.ApplicationConstant;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.StringUtil;

public class ActionOnProductController extends BaseController {
	private IActionOnProductService actionOnProdService = null;
	private IProductService prodService = null;

	public ActionOnProductController() {
		actionOnProdService = new ActionOnProductService();
		prodService = new ProductService();
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Get the action parameter to understand the type of request
		String action = request.getParameter(ApplicationConstant.ACTION);
		String prodId = request.getParameter(ApplicationConstant.prodId);

		// Request come from allProductInfo.jsp,editable product form to be
		// displayed.
		if (action.equals(ApplicationConstant.EDIT_PRODUCT)) {

			List<Jbspit> prodDetails = new ArrayList<Jbspit>();
			IProductService productservice = new ProductService();

			Map<Integer, Map<String, String>> map_prodType = new LinkedHashMap<Integer, Map<String, String>>();
			Map<Integer, Map<String, String>> map_prodSubType = new LinkedHashMap<Integer, Map<String, String>>();

			Map<Integer, Map<String, String>> map_prodColor = new LinkedHashMap<Integer, Map<String, String>>();
			Map<Integer, Map<String, String>> map_prodPolish = new LinkedHashMap<Integer, Map<String, String>>();
			Map<Integer, Map<String, String>> map_prodSize = new LinkedHashMap<Integer, Map<String, String>>();

			try {
				prodDetails = actionOnProdService.getProductOnId(prodId);
				request.setAttribute("prodDetails", prodDetails);

				map_prodType = productservice.getProductsType();
				map_prodSubType = productservice.getProductsSubType();
				map_prodColor = productservice.getProductsColor();
				map_prodPolish = productservice.getProductsPolish();
				map_prodSize = productservice.getProductsSize();

				request.setAttribute("productTypes", map_prodType);
				request.setAttribute("productSubTypes", map_prodSubType);

				request.setAttribute("productColor", map_prodColor);
				request.setAttribute("productPolish", map_prodPolish);
				request.setAttribute("productSize", map_prodSize);
			} catch (ApplicationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (!prodDetails.isEmpty()) {
				rd = request
						.getRequestDispatcher("/pages/jsp/productEntry.jsp");
				rd.forward(request, response);
			}

		}
		// Request come from allProductInfo.jsp,Information about product to be
		// displayed.
		else if (action.equals(ApplicationConstant.INFO_PRODUCT)) {
			List<Jbspit> prodDetails = new ArrayList<Jbspit>();

			prodDetails = actionOnProdService.getProductOnId(prodId);
			request.setAttribute("prodDetails", prodDetails);
			if (!prodDetails.isEmpty()) {
				rd = request
						.getRequestDispatcher("/pages/jsp/productDetails.jsp");
				rd.forward(request, response);
			}

		} else if (StringUtil.isNullOrEmpty(action)
				&& action.equals(ApplicationConstant.DELETE_PRODUCT)) {

			// prodId = request.getParameter("prodId");
			try {
				boolean isDelete = prodService.deleteProduct(prodId);
				if (isDelete) {
					response.sendRedirect("allProductInfo");
				}
			} catch (Exception e) {

			}
		}
	}
}
